package com.mm.fam.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.fam.web.entity.SysDict;
import com.mm.fam.web.mapper.SysDictMapper;
import com.mm.fam.web.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公用字典信息业务
 */
@Service
public class SysDictServiceImpl extends  ServiceImpl<SysDictMapper, SysDict> implements SysDictService
{
    @Autowired
    private SysDictMapper dictMapper;

    @Override
    public IPage<SysDict> queryAllList(Map<String, Object> map, int pageNo, int pageSize) {
        Page<SysDict> page = new Page(pageNo, pageSize);
        map.put("page", page);
        List<SysDict> list = dictMapper.queryAllDict(map);
        page.setRecords(list);
        return page;
    }

    @Override
    public String saveOrUpdateDict(SysDict sysDict) {
        if (sysDict.getId() != null) {
            sysDict.setUpdateDate(new Date());
            dictMapper.updateById(sysDict);
            return "更新操作成功";
        } else {
            sysDict.setCreateDate(new Date());
            sysDict.setDelFlag(true);
            dictMapper.insert(sysDict);
            return "新增操作成功";
        }
    }

    @Override
    public String deleteDict(Integer id) {
        SysDict sysDict = dictMapper.selectById(id);
        sysDict.setUpdateDate(new Date());
        sysDict.setDelFlag(false);
        dictMapper.updateById(sysDict);
        return "删除操作成功";
    }

    @Override
    public List<String> getDictTypes() {
//        Map map = new HashMap();
//        Page<String> page = new Page(pageNo, pageSize);
//        map.put("page", page);
        List<String> list = dictMapper.getDictTypes();
//        page.setRecords(list);
        return list;
    }

    @Override
    public List<SysDict> queryDictByDescribe(Map map) {
        Map newMap = new HashMap();
        Object describe = map.get("describe");
        newMap.put("`describe`",describe);
        newMap.put("del_flag",1);
        newMap.put("status",1);
        List<SysDict> list = dictMapper.selectByMap(newMap);
        if("入金".equals(describe)){
            for (SysDict sysDict:list){
                if("开立账户".equals(sysDict.getDictValue())){
                    list.remove(sysDict);
                    break;
                }
            }
        }
        return list;
    }

    @Override
    public List<SysDict> findDictByType(String type) {
        QueryWrapper<SysDict> qw = new QueryWrapper<>();
        qw.eq("status",1);
        qw.eq("type",type);
        qw.eq("del_flag",1);
        return dictMapper.selectList(qw);
    }


}
